package com.foreverwin.jzymes.websocket;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.foreverwin.jzymes.common.service.IActivityRuleAdviceService;
import com.foreverwin.jzymes.common.service.IProductionShiftService;
import com.foreverwin.jzymes.common.service.IResourceAdviceService;
import com.foreverwin.jzymes.common.service.ISystemRuleAdviceService;
import com.foreverwin.jzymes.framework.annotation.WebSocketHandler;
import com.foreverwin.jzymes.framework.event.AppEvent;
import com.foreverwin.jzymes.framework.websocket.TextMessageWebSocketWraper;
import com.foreverwin.jzymes.production.inspect.domain.InspectAlarmLog;
import com.foreverwin.jzymes.production.inspect.mapper.InspectAlarmLogAdviceMapper;
import com.foreverwin.jzymes.production.inspect.service.IInspectAlarmLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;

import java.util.*;

@WebSocketHandler(channel = "Z_DISMANTLE_ALARM")
public class DismantleAlarmHandler extends TextMessageWebSocketWraper {

    @Autowired
    IActivityRuleAdviceService activityRuleAdviceService;
    @Autowired
    IInspectAlarmLogService inspectAlarmLogService;
    @Autowired
    ISystemRuleAdviceService systemRuleAdviceService;
    @Autowired
    IProductionShiftService productionShiftService;
    @Autowired
    IResourceAdviceService resourceAdviceService;
    @Autowired
    InspectAlarmLogAdviceMapper inspectAlarmLogAdviceMapper;
    @Autowired
    JmsMessagingTemplate jmsTemplate;

    private Map<String,String> activityRuleMap;

    public DismantleAlarmHandler(){
        events = new ArrayList<>();
        events.add( "DISMANTLE_ALARM" );
    }

    @Override
    public void doAfterOpen(Connection connection) {
        activityRuleMap = activityRuleAdviceService.findActivityRules( getChannel() );
    }

    @Override
    public void doAfterClose() {

    }

    @Override
    public void onAppEvent(AppEvent appEvent) {
        JSONObject eventData = (JSONObject) appEvent.getData();
        String site = activityRuleMap.get( "SITE" );
        String resrce = eventData.getString( "RESRCE" );
        String operation = resourceAdviceService.findOperationByResource( site, resrce );
        JSONObject responseBody = findInspectAlarmLogInfo( site, operation );
        sendMessage( "WS_DISMANTLE_ALARM_INFRA", responseBody );
    }

    @Override
    public JSONObject doReceiveAndReply( String messageType, JSONObject requestParams ) {

        JSONObject responseBody = null;
        if( "WS_INIT_DATA".equals( messageType ) ){
            String site = activityRuleMap.get( "SITE" );
            String operation = requestParams.getString( "OPERATION" );
            responseBody = findInspectAlarmLogInfo( site, operation );
        }

        if( "WS_CLEAR_DISMANTLE_ALARM_INFRA".equals( messageType ) ){
            String site = activityRuleMap.get( "SITE" );
            String carrier = requestParams.getString( "CARRIER" );
            String operation = requestParams.getString( "OPERATION" );
            updateInspectAlarmLog( site, carrier, operation );
            responseBody = findInspectAlarmLogInfo( site, operation );
        }

        return responseBody;
    }

    /**
     * 修改拆夹报警记录
     *
     */
    public void updateInspectAlarmLog( String site, String carrier, String operation ){

        InspectAlarmLog inspectAlarmLog = inspectAlarmLogAdviceMapper.selectInspectAlarmLogByCarrierDate( site, carrier, operation );
        if( inspectAlarmLog != null ){
            inspectAlarmLog.setDisposition( "Y" );
            inspectAlarmLog.setUpdatedDateTime( new Date() );
            inspectAlarmLogService.updateInspectAlarmLog( inspectAlarmLog );
        }
    }

    /**
     * 拆夹报警看板数据
     *
     * @param site
     * @return
     */
    /**
     * 拆夹报警看板数据
     *
     * @return
     */
    public JSONObject findInspectAlarmLogInfo( String site, String operation ){

        Date[] dateRange = productionShiftService.findShiftRange( site, new Date() );

        List<InspectAlarmLog> completeLog = inspectAlarmLogAdviceMapper.selectInspectAlarmLogByDate( site, operation,"Y", dateRange[0], dateRange[1] );
        List<String> completeList = new ArrayList<>();
        for( InspectAlarmLog inspectAlarmLog : completeLog ){
            if( completeList.size() < 12 ){
                completeList.add( inspectAlarmLog.getCarrier() );
            }else{
                break;
            }
        }

        List<InspectAlarmLog> uncompleteLog = inspectAlarmLogAdviceMapper.selectInspectAlarmLogByDate( site, operation,"N", dateRange[0], dateRange[1] );
        List<Map<String,Object>> uncompleteList = new ArrayList<>();
        for( InspectAlarmLog inspectAlarmLog : uncompleteLog ){
            if( uncompleteList.size() < 4 ){
                Map<String,Object> uncompleteMap = new HashMap<>();
                uncompleteMap.put( "text", inspectAlarmLog.getCarrier() );
                uncompleteMap.put( "ncList", inspectAlarmLog.getAlarmMsg() );
                try{
                    uncompleteMap.put( "ncList", JSON.parseArray( inspectAlarmLog.getAlarmMsg() ) );
                }catch ( Exception e1 ){
                    uncompleteMap.put( "ncList", inspectAlarmLog.getAlarmMsg() );
                }
                uncompleteList.add( uncompleteMap );
            }else{
                break;
            }
        }

        JSONObject responseBody = new JSONObject();
        responseBody.put( "completeQty", completeLog.size() );
        responseBody.put( "complete", completeList );
        responseBody.put( "needDealQty", uncompleteLog.size() );
        responseBody.put( "needDeal", uncompleteList );

        return responseBody;
    }
}
